var jq = jQuery.noConflict();

jQuery(document).ready(function() {
    $(function () {
        //加载头部
        $("#tou").load("Header.html",function () {
            $(".leftNav").attr("class","leftNav none");

            $(".nav").hover(function () {
                $(".leftNav").attr("class","leftNav");
            },function () {
                $(".leftNav").attr("class","leftNav none");
            })
        });

        //商品内容
        xiangqing();

        //商品加购
        addtocar();

        //加收藏
        addToFavorites();

        $("[name='number']").change(function (){
            var reg=/^\d{1,}$/;
            var num=$(this).val();
            if (reg.test(num)==false){
                alert("输入必须为正数!");
                $(this).val("1");
            }
        });
    });

})

//商品内容
function xiangqing(){
    var id = getQueryString("id");

    $.ajax({
        url:"easybuy/product",
        data:{
            productId:id
        },
        success:function (product) {
            if (product==null||product==""){
                $(".pro_des").html("<h1>该商品已下架！！！</h1>");
            }else {
                //商品名称
                $(".des_name").html("<p>"+product.name+"</p>\n" +product.description);
                //价格
                $(".des_price").children("b").html("￥"+product.price);
                //图片
                $("#tsImgS").children("a").attr("href","http://121.37.168.26:9999/ftpfile/"+product.filename);
                $("#MagicZoom").children("img").attr("src","http://121.37.168.26:9999/ftpfile/"+product.filename);
                $("#tsImgSCon").find("li").eq(0).children("img").attr("src","http://121.37.168.26:9999/ftpfile/"+product.filename);
                $(".des_join").find("img").attr("data-id",product.id);
                $("#stock").html("库存: "+product.stock);
            }
        }
    })
}

//加购
function addtocar() {
    var jiagou = $(".des_join").find("img");
    jiagou.click(function (){
        var id = $(this).data("id");
        var addCount = $(".n_ipt").val();
        $.ajax({
            url:"easybuy/caradd",
            data:{
                productId:id,
                addCount:addCount
            },
            success:function (a){
                if (a == "userEmpty"){
                    ShowDiv_1("nologin","nologin");
                }else if (a =="shortage"){
                    alert("库存不足！");
                }else {
                    ShowDiv_1("fade1","MyDiv1");
                    car();
                }
            },
            error:function () {
                alert(1);
            }
        })
    });
}

//取得地址栏参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg); //r是一个数组
    if (r != null) return unescape(r[2]); return null;}

//点击加购后显示加购成功或未登录
function ShowDiv_1(show_div,bg_div){
    document.getElementById(show_div).style.display='block';
    document.getElementById(bg_div).style.display='block' ;
    var bgdiv = document.getElementById(bg_div);
    bgdiv.style.width = document.body.scrollWidth;
    // bgdiv.style.height = $(document).height();
    $("#"+bg_div).height($(document).height());
};

//关闭加购成功或未登录页面
function CloseDiv_1(show_div,bg_div)
{
    document.getElementById(show_div).style.display='none';
    document.getElementById(bg_div).style.display='none';
};

//加收藏
function addToFavorites() {
    var id = getQueryString("id");
    $(".d_care").click(function (){
        $.ajax({
            url:"easybuy/favoritesadd",
            data:{
                productId:id
            },
            success:function (result){
                if (result == "userEmpty"){
                    alert("请先登录！");
                }else if (result == "exists"){
                    alert("您已收藏过该商品");
                } else{
                    // alert("添加成功！");
                    ShowDiv('MyDiv','fade');
                    car();
                }
            }
        })
    });

}